﻿@inject DialogService DialogService
@inject IMaterialService MaterialService

<RadzenStack Gap="1.5rem">
    <RadzenTemplateForm TItem="SaleOrderDetailEditRequestDto" Data="@_editRequestDto" Submit="OnSubmit">
        <RadzenStack class="rz-mb-2">
            <RadzenRow>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="物料编号" Style="width: 100%;">
                        <RadzenDropDownDataGrid TValue="int" LoadData=@LoadData AllowFiltering="true"
                                                Data="_materials" Count="_count"
                                                TextProperty="Code" ValueProperty="Id"
                                                Value="_editRequestDto.MaterialId" ValueChanged="MaterialValueChanged">
                            <Columns>
                                <RadzenDropDownDataGridColumn Property="Code" Title="Code" Width="100px" />
                                <RadzenDropDownDataGridColumn Property="Name" Title="Name" Width="100px" />
                            </Columns>
                        </RadzenDropDownDataGrid>
                    </RadzenFormField>
                </RadzenColumn>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="不含税单价" Style="width: 100%;">
                        <RadzenNumeric @bind-Value="_editRequestDto.ExcludingTaxPrice"></RadzenNumeric>
                    </RadzenFormField>
                </RadzenColumn>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="税率" Style="width: 100%;">
                        <RadzenNumeric @bind-Value="_editRequestDto.Tax"></RadzenNumeric>
                    </RadzenFormField>
                </RadzenColumn>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="数量" Style="width: 100%;">
                        <RadzenNumeric @bind-Value="_editRequestDto.Quantity"></RadzenNumeric>
                    </RadzenFormField>
                </RadzenColumn>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="交货时间" Style="width: 100%;">
                        <RadzenDatePicker @bind-Value="_editRequestDto.DeliveryTime"></RadzenDatePicker>
                    </RadzenFormField>
                </RadzenColumn>
            </RadzenRow>
        </RadzenStack>
        <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Gap="0.5rem">
            <RadzenButton ButtonStyle="ButtonStyle.Light" Text="Cancel" Variant="Variant.Flat" Click="@CancelButtonClick" />
            <RadzenButton ButtonStyle="ButtonStyle.Primary" ButtonType="ButtonType.Submit" Icon="save" Text="Save" Variant="Variant.Flat" />
        </RadzenStack>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    SaleOrderDetailEditRequestDto _editRequestDto = new();
    private IList<MaterialInfoResponseDto> _materials = new List<MaterialInfoResponseDto>();
    private int _count = 0;

    async Task LoadData(LoadDataArgs args)
    {
        var result = await MaterialService.QueryMaterialsByPage(new MaterialSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _materials = result.Data!;
            _count = result.TotalCount;
        }

        await InvokeAsync(StateHasChanged);
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var result = await MaterialService.QueryMaterialsByPage(new MaterialSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _materials = result.Data!;
            _count = result.TotalCount;
        }
    }

    void MaterialValueChanged(int materialId)
    {
        var materialInfoResponseDto = _materials.FirstOrDefault(i => i.Id == materialId);
        if (materialInfoResponseDto != null)
        {
            _editRequestDto.MaterialCode = materialInfoResponseDto.Code;
            _editRequestDto.MaterialName = materialInfoResponseDto.Name;
        }
    }

    void CancelButtonClick()
    {
        DialogService.Close();
    }

    void OnSubmit()
    {
        DialogService.Close(_editRequestDto);
    }

}